<template>
    <div class="myAnswerPage">
        <a-form
                :model="formSearchParams"
                :style="{ marginBottom: '20px' }"
                layout="inline"
                @submit="doSearch"
        >
            <a-form-item field="resultName" label="结果名称">
                <a-input
                        v-model="formSearchParams.resultName"
                        placeholder="请输入结果名称"
                        allow-clear
                />
            </a-form-item>
            <a-form-item field="resultDesc" label="结果描述">
                <a-input
                        v-model="formSearchParams.resultDesc"
                        placeholder="请输入结果描述"
                        allow-clear
                />
            </a-form-item>
            <a-form-item field="appId" label="应用 id">
                <a-input
                        v-model="formSearchParams.appId"
                        placeholder="请输入应用 id"
                        allow-clear
                />
            </a-form-item>
            <a-form-item>
                <a-button type="primary" html-type="submit" style="width: 100px">
                    搜索
                </a-button>
            </a-form-item>
        </a-form>
        <a-table
                :columns="columns"
                :data="dataList"
                :pagination="{
      showTotal: true,
      pageSize: searchParam.pageSize,
      current: searchParam.current,
      total,
    }"
                @page-change="onPageChange"
        >
            <template #resultPicture="{ record }">
                <a-image width="64" :src="record.resultPicture ?? myResultPicture" />
            </template>
            <template #appType="{ record }">
                <a-tag v-if="record.appType === 0" color="magenta" bordered>{{APPTYPE_MAP[record.appType]}}</a-tag>
                <a-tag v-if="record.appType === 1" color="purple" bordered>{{APPTYPE_MAP[record.appType]}}</a-tag>

            </template>
            <template #scoringStrategy="{ record }">
                <a-tag v-if="record.scoringStrategy === 0" color="cyan" bordered>{{SCORINGTYPE_MAP[record.scoringStrategy]}}</a-tag>
                <a-tag v-if="record.scoringStrategy === 1" color="gold" bordered>{{SCORINGTYPE_MAP[record.scoringStrategy]}}</a-tag>

            </template>
            <template #createTime="{ record }">
                {{ dayjs(record.createTime).format("YYYY-MM-DD HH:mm:ss") }}
            </template>
            <template #optional="{ record }">
                <a-space>
                    <a-button status="danger" @click="doDelete(record)">删除</a-button>
                </a-space>
            </template>
        </a-table>
    </div>
</template>

<script setup lang="ts">
    import {reactive,ref,watchEffect} from 'vue';
    import {deleteUserUsingPost, listUserByPageUsingPost} from "@/api/userController";
    import message from "@arco-design/web-vue/es/message"
    import { dayjs } from "@arco-design/web-vue/es/_utils/date";
    import {deleteUserAnswerUsingPost, listUserAnswerVoByPageUsingPost} from "@/api/userAnswerController";
    import {APPTYPE_MAP,SCORINGTYPE_MAP} from "@/constant/enum";
    import {myResultPicture} from '@/constant/photo'

    // 表格列配置
    const columns = [
        {
            title: "id",
            dataIndex: "id",
        },
        {
            title: "选项",
            dataIndex: "choices",
        },
        {
            title: "结果 id",
            dataIndex: "resultId",
        },
        {
            title: "名称",
            dataIndex: "resultName",
        },
        {
            title: "描述",
            dataIndex: "resultDesc",
        },
        {
            title: "图片",
            dataIndex: "resultPicture",
            slotName: "resultPicture",
        },
        {
            title: "得分",
            dataIndex: "resultScore",
        },
        {
            title: "应用 id",
            dataIndex: "appId",
        },
        {
            title: "应用类型",
            dataIndex: "appType",
            slotName: "appType",
        },
        {
            title: "评分策略",
            dataIndex: "scoringStrategy",
            slotName: "scoringStrategy",
        },
        {
            title: "创建时间",
            dataIndex: "createTime",
            slotName: "createTime",
        },
        {
            title: "操作",
            slotName: "optional",
        },
    ];

    //用于搜索的变量
    const formSearchParams = ref<API.UserQueryRequest>({});

    // 初始化搜索条件（不应该被修改）
    const initSearchParam = ref<API.UserQueryRequest>({
        pageSize:10,
        current:1
    })
    const searchParam = ref({
        ...initSearchParam.value
    })

    //表格数据
    const dataList = ref<API.UserAnswerVO[]>([])
    //定义总条数
    const total = ref<number>(0)

    /**
     * 执行搜索
     */
    const doSearch = () => {
        searchParam.value = {
            ...initSearchParam,
            ...formSearchParams.value,
        };
    };

    //获取数据
    const loadData = async() =>{
        const res = await listUserAnswerVoByPageUsingPost(searchParam.value);
        if(res.data.code === 0){
            dataList.value = res.data.data?.records || [];
            total.value = res.data.data?.total || 0;
        }else{
            message.error("获取数据失败" + res.data.message)
        }
    }

    //改页数事件
    const onPageChange = (page:number) =>{
        searchParam.value = {
            ...searchParam.value,
            current:page
        }
    }



    /**
     * 删除
     * @param record
     */
    const doDelete = async (record: API.User) => {
        if (!record.id) {
            return;
        }

        const res = await deleteUserAnswerUsingPost({
            id: record.id,
        });
        if (res.data.code === 0) {
            loadData();
        } else {
            message.error("删除失败，" + res.data.message);
        }
    };

    /**
     * 监听 searchParam 变量，改变时触发数据的重新加载
     */
    watchEffect(() =>{
        loadData()
    })
</script>

<style scoped>

</style>